package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.AddDeliveryVo;
import com.woniuxy.oldmanapi.cvo.CardVo;
import com.woniuxy.oldmanapi.cvo.DeliveryDetailVo;
import com.woniuxy.oldmanapi.cvo.DeliveryVo;
import com.woniuxy.oldmanapi.entity.Card;
import com.woniuxy.oldmanapi.entity.Delivery;
import com.woniuxy.oldmanapi.entity.DeliveryMedicineBill;
import com.woniuxy.oldmanapi.service.DeliveryDetailService;
import com.woniuxy.oldmanapi.service.DeliveryService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
public class DeliveryController {
    @Autowired
    DeliveryService deliveryService;
    @Autowired
    DeliveryDetailService deliveryDetailService;

    //查询
    @GetMapping("/delivery/searchList")
    public ResponseResult<PageInfo<DeliveryVo>> list(String oldername,
                                                   @RequestParam(value = "state", defaultValue = "-1") int state,
                                                   @RequestParam(value = "size", defaultValue = "6") int size,
                                                   @RequestParam(value = "now", defaultValue = "1") int now) {

        ResponseResult<PageInfo<DeliveryVo>> result = new ResponseResult<>();
        try {
            PageInfo<DeliveryVo> pageInfo = deliveryService.findDeliverysByCons(oldername, state, size, now);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);

        }
        return result;
    }
    //作废
    @GetMapping("/delivery/cancel/{id}")
    public ResponseResult<Void> cancel(@PathVariable("id") int id) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            deliveryService.cancelDelivery(id);
            result.setMsg("作废成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("作废失败");
            result.setCode(201);

        }
        return result;
    }

    //出库
    @GetMapping("/delivery/gopay")
    public ResponseResult<Void> deliveryPay(int id,BigDecimal pay,String CardNumber) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            deliveryService.DeliveryById(id,pay,CardNumber);
            result.setMsg("出库成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setCode(201);

        }
        return result;
    }


    //查看订单详情
    @GetMapping("/delivery/detailById/{id}")
    public ResponseResult<List<DeliveryDetailVo>> detailById(@PathVariable("id")int id) {

        ResponseResult<List<DeliveryDetailVo>> result = new ResponseResult<>();
        try {
            List<DeliveryDetailVo> list= deliveryDetailService.findDeliveryDetailByDid(id);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);
        }
        return result;
    }

    //查询全部IC卡(姓名和卡号)
    @GetMapping("/card/findAllCard")
    public ResponseResult<List<CardVo>> findAllCard( String cardName) {

        ResponseResult<List<CardVo>> result = new ResponseResult<>();
        try {
            List<CardVo> list= deliveryDetailService.findAllCardvoByCard(cardName);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);
        }
        return result;
    }

    //添加==药品出库单
    @PostMapping("/delivery/add")
    public ResponseResult<Void> adddelivary(@RequestBody AddDeliveryVo addDeliveryVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
             deliveryService.addDelivery(addDeliveryVo);
            System.out.println(addDeliveryVo.getDeliveryDetailVos()+"ppp");
            System.out.println(addDeliveryVo.getTotalprice());
            result.setMsg("添加成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("添加失败");
            result.setCode(201);
        }
        return result;
    }


    //编辑==药品出库单
    @PostMapping("/delivery/modify")
    public ResponseResult<Void> modifyDelivaly(@RequestBody AddDeliveryVo addDeliveryVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            deliveryService.modifyDelivery(addDeliveryVo);
            result.setMsg("修改成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("修改失败");
            result.setCode(201);
        }
        return result;
    }


}
